package org.example;

import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;

/**
 * 从源 minio 地址下载指定文档，上传到目标 minio 上，bucket 和 文件名保持不变
 * @author wangwz
 * @date 2024/5/24
 */
public class DownloadFileAndUploadMinio {
    public static void main(String[] args) throws IOException, NoSuchAlgorithmException, MinioException {
        // 目标MinIO服务配置
        String endpoint = "http://toip:7042";
        String accessKey = "admin";
        String secretKey = "bjtcrj123456";
        String bucketName = "tcboot-ls"; // 存储桶名称

        // 创建MinIO客户端
        MinioClient minioClient = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();

        // 从源 minio 下载文档
        String docNames = "http://fromip:7042/tcboot-ls/zfdoctemplate/现场检查勘验笔录_1713336288457.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/责令停止改正违法行为通知书_1713343112637.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/责令改正情况复查记录_1713346813625.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/当场行政处罚决定书_1713347373025.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/行政处罚决定书_1713347117815.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/行政执法线索移送书回执_1713347859590.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/技术鉴定结果告知书17029652133901703661855610_1713348208910.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/函17029652892081703661828529_1713348274379.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/案件调查终结报告1702965328781_1703658114238.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/行政处罚事先告知书17029596840601703664861362_1713348435325.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/行政处罚决定法制审核意见表1702965420086_1703664312968.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/送达地址确认书17029654926941703663577140_1713348711286.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/送达回执1702965593414_1703663609457.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/不予行政处罚决定书1702966073319_1703661517199.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/行政案件移送书1702966191532_1703663839010.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/涉嫌犯罪案件移送书1702966378146_1703663550711.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/行政执法监督案件结案报告1702966490742_1703666164532.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/行政强制措施现场笔录1702966587563_1703665442175.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/当场行政处罚决定书_1704162474194.doc,http://fromip:7042/tcboot-ls/zfdoctemplate/行政执法监督案卷目录1702956242666_1703666306077.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/涉案物品清单1702957562494_1703663518764.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/行政强制措施现场笔录1702956689935_1703665429589.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/案卷目录1702956496229_1703661483527.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/抽样取证通知书_1704161544544.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/责令改正情况复查记录1702957604524_1703666401824.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/听证报告_1704161418025.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/证据清单1702956440687_1703666587644.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/行政执法监督案件结案报告1702956834228_1703666154224.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/行政处罚听证通知书_1704161864119.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/行政执法监督案卷首页_1704161437349.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/抽样取证记录1702958511451_1703661544907.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/副页1702955905006_1703661794662.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/证据资料1702957485482_1703666599180.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/行政处罚决定书1702957098128_1703664783542.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/涉嫌犯罪案件移送书1702956956217_1703663542708.docx,http://fromip:7042/tcboot-ls/zfdoctemplate/当场行政处罚决定书1702957218415_1703661679706.docx";
        List<String> docList = Arrays.asList(docNames.split(","));
        for (String docPath : docList) {
            File downloadedFile = downloadFile(docPath);
            String objectName = docPath.replace("http://fromip:7042/tcboot-ls/", "");
            try {
                // 上传文件到MinIO
                minioClient.uploadObject(
                        UploadObjectArgs.builder()
                                .bucket(bucketName)
                                .object(objectName)
                                .filename(downloadedFile.getAbsolutePath())
                                .build());
                System.out.println("文件上传成功");
            } catch (InvalidKeyException e) {
                throw new RuntimeException(e);
            } finally {
                // 清理本地临时文件
                if (downloadedFile.exists()) {
                    downloadedFile.delete();
                }
            }
        }
    }

    private static File downloadFile(String urlString) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(urlString)
                .build();
        try (Response response = client.newCall(request).execute();
             ResponseBody body = response.body()) {
            if (!response.isSuccessful() || body == null) {
                throw new IOException("Unexpected code " + response);
            }

            // 保存到临时文件
            String fileName = urlString.substring(urlString.lastIndexOf("/") + 1);
            File outputFile = new File(System.getProperty("user.dir") + File.separator + fileName);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            try (InputStream in = body.byteStream();
                 OutputStream out = new FileOutputStream(outputFile)) {
                byte[] buffer = new byte[1024];
                int read;
                while ((read = in.read(buffer)) != -1) {
                    out.write(buffer, 0, read);
                }
            }
            return outputFile;
        }
    }
}
